package com.easylinkin.sm.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.sm.constant.LogConstant;
import com.easylinkin.sm.constant.LogConstant.Fields;
import com.easylinkin.sm.constant.LogConstant.Module;
import com.easylinkin.sm.entity.Log;
import com.easylinkin.sm.mapper.LogMapper;
import com.easylinkin.sm.repository.LogRepository;
import com.easylinkin.sm.service.LogService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.morn.boot.data.DisplayableServiceSupport;
import site.morn.boot.rest.RestPage;
import site.morn.rest.RestPageable;

/**
 * 日志服务
 *
 * @author yezhihao
 * @since develop, 2019/8/19
 */
@Slf4j
@Service("logServiceImpl")
@Transactional
public class LogServiceImpl extends DisplayableServiceSupport<Log, Long, LogRepository> implements
    LogService {

  @Resource
  private LogMapper logMapper;

  @Override
  public List<Map<String, Object>> getLogModules() {
    Locale locale = LocaleContextHolder.getLocale();
    String language =  locale.toString().toUpperCase();
    List<Map<String, Object>> modules = logMapper.getLogModules(Module.LOG, language);
    return removeRepeatMapByKey(modules, LogConstant.Fields.MODULEID);
  }

  /**
   * 日志分页查询 过滤条件:登录用户所属客户，创建时间为最近一个月的
   */
  @Override
  public Page<Log> getAllLog(
      RestPage<Log> restPage) {
    RestPageable pageable = restPage.getPageable();
    Log log = restPage.getModel();
    Page page = new Page((long)(pageable.getPage() + 1), pageable.getSize());
    String beginTime = "";
    String endTime = "";
    if (restPage.getAttach().get(Fields.BEGINTIME) != null) {
      beginTime = restPage.getAttach().get(Fields.BEGINTIME).toString();
      endTime = restPage.getAttach().get(Fields.ENDTIME).toString();
    }
    page.setRecords(logMapper
        .getAllLog(log.getModifier(), log.getCustomerId(), log.getModuleId(), log.getStatus(),
            beginTime,
            endTime, page));
    return page;
  }

  /**
   * 根据日志id查询日志详情
   */
  @Override
  public Map<String,String> getDetails(Long id) {
    return logMapper.getDetails(id);
  }

  /**
   * 根据map中的某个key 去除List中重复的map
   *
   * @author yezhihao
   */
  public static List<Map<String, Object>> removeRepeatMapByKey(List<Map<String, Object>>
      list, String mapKey) {
    if (list.isEmpty()) {
      return list;
    }
    //把list中的数据转换成msp,去掉同一id值多余数据，保留查找到第一个id值对应的数据
    List<Map<String, Object>> listMap = new ArrayList<>();
    Map<Long, Map> msp = new HashMap<>();
    for (int i = list.size() - 1; i >= 0; i--) {
      Map map = list.get(i);
      Long id = (Long) map.get(mapKey);
      map.remove(mapKey);
      msp.put(id, map);
    }
    //把msp再转换成list,就会得到根据某一字段去掉重复的数据的List<Map>
    Set<Long> mspKey = msp.keySet();
    for (Long key : mspKey) {
      Map newMap = msp.get(key);
      newMap.put(mapKey, key);
      listMap.add(newMap);
    }
    return listMap;
  }

}
